
#include <memory.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>


#include "radio.h"
#include "radio_config.h"

/*
PI0  :	Program is running, led 
PI0  :	Tx status,  led
PI2  :	Rx status,  led
PI3  :	prepare to tx, switch on,  after finish tx, switch off 

PI4 :
PI5 :
PI6 :

PI7  :	button : start capture picture and send data.          input

PI8 :
--
PI15 :

*/
#define PI_CFG0_REG         		(0xf1c20800+0x120)
#define PI_DAT_REG         			(0xf1c20800+0x130)

#define PI_PROGAM_RUN(bset)  		PI0_6_WRITE_BIT(0, bset)
#define PI_PROGAM_TX(bset)  		PI0_6_WRITE_BIT(1, bset)
#define PI_PROGAM_RX(bset)  		PI0_6_WRITE_BIT(2, bset)
#define PI_PROGAM_TXON(bset)  		PI0_6_WRITE_BIT(3, bset)

#define PI_PROGAM_TRIG_TX(bset)  		( memRead(PI_DAT_REG) & (1 << 7) )
	
#define PA_CFG0_REG           		0xf1c20800
#define PA_DAT_REG         	  	0xf1c20810		

#define PB_CFG1_REG        	  0xf1c20828		
#define PG_CFG0_REG        	(0xf1c208D8)
#define PG_CFG1_REG        	(0xf1c208DC)
#define PG_DAT_REG         	 0xf1c208E8	



int g_memFd = -1;

char devName[126];
int buf[4];

#define RET_NUM   2
#define FILE_LEN   12


inline unsigned int memRead(unsigned int addr)  
{
	return read( g_memFd, &addr, 4);
}

inline void memWrite( unsigned int addr, int value)  
{
	buf[0] = addr;
	buf[1] = value;
	write( g_memFd, &buf, 8);
}

inline void SPI_WRITE_BIT( int valueBit, int bSet) 
{
	static unsigned int P_Data = 0;
	#ifdef  CORE_BOARD 
	{
		if( P_Data == 0)
			P_Data = memRead( PA_DAT_REG ) ;
		if( bSet )
			P_Data |= (1 << valueBit);
		else
			P_Data &= (~(1 << valueBit) );
		memWrite( PA_DAT_REG ,  P_Data  ) ;
		
	}
	#endif
	#ifdef  DEV_BOARD 
	{
		if( P_Data == 0)
			P_Data = memRead( PG_DAT_REG ) ;
		if( bSet )
			P_Data |= (1 << valueBit);
		else
			P_Data &= (~(1 << valueBit) );
		memWrite( PG_DAT_REG ,  P_Data ) ;
	}
	#endif

}

inline void PI0_6_WRITE_BIT( int valueBit, int bSet) 
{
	static unsigned int PI_Data = 0;

	{
		if( PI_Data == 0)
			PI_Data = memRead( PI_DAT_REG ) ;
		if( bSet )
			PI_Data |= (1 << valueBit);
		else
			PI_Data &= (~(1 << valueBit) );
		memWrite( PI_DAT_REG ,  PI_Data ) ;
	}

}


/*
          rx                                tx(develop board)
SDN   3   PA9(CKO2)                     	PG0(PCLK)

NSEL  4   PA5(SPI3_CS0)                 	PG2(HSYNC)

SCK   5   PA6(SPI3_CLK)                 	PG4(D0)

SDI   6   PA7(SPI3_MOSI)                 	PG6(D2)

SD0   8   PA8(SPI3_MISO)   input           	PG8(D4)

NIRQ  7   PB10             input           	PG10(D6)

*/


int  openMemDev()
{
	unsigned int value;
    if( g_memFd == -1)
    {
        if((g_memFd = open(devName, O_RDWR | O_SYNC)) == -1)
        {
            printf("open %s error\n", devName );
			return -1;
        }
	#ifdef CORE_BOARD  //#defined in radio.h
		// mainboard
		value = memRead( PA_CFG0_REG ) ;
		value = (value & 0x000fffff ) | 0x11100000;//pa5,6,7
		memWrite( PA_CFG0_REG , value ) ;
 		value = memRead( PA_CFG0_REG + 4) ;
		value = (value & 0xffffff00 ) | 0x00000010;//pa8:input , 9: output
		memWrite( PA_CFG0_REG + 4 , value ) ;

		value = memRead( PB_CFG1_REG ) ;
		value = (value & 0xFFFff0ff ) ; //pB10:IRQ
		memWrite( PB_CFG1_REG , value ) ; 
	#else
		// dev board
		value = memRead( PG_CFG0_REG ) ;
		value = (value & 0xF0F0F0F0 ) | 0x01010101;//PG0-,PG2, PG4, PG6  OUTPUT, PG8,PG10:INPUT
		memWrite( PG_CFG0_REG , value ) ;

		value = memRead( PG_CFG1_REG ) ;
		value = (value & 0xFFFFF0F0 ) ;// PG8,PG10:INPUT
		memWrite( PG_CFG1_REG , value ) ;  
	#endif
#ifdef GPIO_BOARD
	memWrite( PI_CFG0_REG , 0x01111111 ) ;  //PI0 -,PI6: OUTPUT,   PI7 : INPUT
#endif
}
	return 0;
}

 extern tRadioConfiguration * pRadioConfiguration;
 extern  unsigned char (* pRecvBuf) [RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH];
 extern unsigned char JpgTransBuf1[ 49152 ][RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH];  //max jpg size: 6M

 #define PACKET_SEND_INTERVAL  1000u


	
	int main(int argc,char *argv[])
	{

/*	if( argc < 2 )
	{
		printf("name,dev (like:memdis /dev/mem) \n");
		return 0;
	}*/
	int  led = 0;
	strcpy(devName , "/dev/chrdev" );

	if( openMemDev() < 0)
		return 0;
	
	   short i = 0;
	   for(i = 0; i < 32; i++)
		pRadioConfiguration->Radio_CustomPayload[i] = i + 1;
	   
	   vRadio_Init();

 // Start RX
    si446x_part_info();
  	usleep(1000);
  vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber);
	pRecvBuf = JpgTransBuf1;
	
	
	while ( 1 )
	{
#ifdef GPIO_BOARD
		if( led % 256 == 0)
			PI_PROGAM_RUN( 1 );
		else if( led % 256 == 128)
			PI_PROGAM_RUN( 0 );
#endif
	// Demo Application Poll-Handler function
		gRadio_CheckReceived_VariablePacket() ;
	}
	
	
	}

    

